/*******************************************************************************
 * Copyright (c) 2011 Prometheus Consulting
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/

package nz.co.senanque.vaadinsupport.viewmanager;

import java.util.HashMap;
import java.util.Map;

import javax.security.auth.login.LoginException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.MessageSourceAware;
import org.springframework.context.support.MessageSourceAccessor;

import com.vaadin.addon.touchkit.ui.TouchKitApplication;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Component.Event;
import com.vaadin.ui.Form;
import com.vaadin.ui.LoginForm;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Window.Notification;

/**
 * 
 * @author Roger Parkinson
 * @version $Revision:$
 */

public class TouchLoginForm extends Form implements InitializingBean,
		MessageSourceAware {
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	private static final long serialVersionUID = 8927668036479913166L;
	String usernameCaption;
	String passwordCaption;
	String submitCaption;
	String welcomeCaption;
	private MessageSourceAccessor m_messageSourceAccessor;
	@Autowired
	private TouchLoginListener m_loginListener;

	public TouchLoginForm() {
	}

	public TouchLoginListener getLoginListener() {
		return m_loginListener;
	}

	public void setLoginListener(TouchLoginListener loginListener) {
		m_loginListener = loginListener;
	}

	public void setMessageSource(MessageSource messageSource) {
		m_messageSourceAccessor = new MessageSourceAccessor(messageSource);
	}

	public void afterPropertiesSet() throws Exception {
		usernameCaption = m_messageSourceAccessor.getMessage("username");
		passwordCaption = m_messageSourceAccessor.getMessage("password");
		submitCaption = m_messageSourceAccessor.getMessage("login.button");
		welcomeCaption = m_messageSourceAccessor.getMessage("welcome");
		final TextField userName = new TextField(usernameCaption);
		userName.setImmediate(true);
		addField("userName", userName);
		final PasswordField password = new PasswordField(passwordCaption);
		password.setImmediate(true);
		addField("password", password);
		Button submit = new Button(submitCaption);
		addField("submit", submit);
		submit.addListener(new ClickListener() {

			private static final long serialVersionUID = 5201900702970450254L;

			public void buttonClick(ClickEvent event) {
				Map<String,String> map = new HashMap<String,String>();
				map.put("user",String.valueOf(userName.getValue()));
				map.put("password",String.valueOf(password.getValue()));
				if (getLoginListener() != null) {
					try {
						getLoginListener().onLogin(map);
					} catch (Exception e) {
						Throwable cause = e.getCause();
						if (cause == null || !(cause instanceof LoginException)) {
							logger.error(e.getMessage(), e);
						}
						String message = m_messageSourceAccessor.getMessage(
								"Bad.Login", "Bad Login");
						TouchKitApplication.get().getMainWindow().showNotification(message,Notification.TYPE_ERROR_MESSAGE);
						return;
					}
				}
				TouchKitApplication.get().setUser(userName.getValue());
			}
		});
	}
}
